package com.quanquan.user.controller;

import DTO.PageDTO;
import com.quanquan.user.dto.SignInRuleDTO;
import com.quanquan.user.entity.SignInRule;
import com.quanquan.user.exception.SignInRuleException;
import com.quanquan.user.request.SignRuleInRequestBody;
import com.quanquan.user.service.SignInRuleService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import request.PageRequestBody;
import utils.StringUtils;
import web.response.ResultBody;

/**
 * 功能描述:
 *
 * @ClassName: SignInRuleController
 * @Author: xiaoqianh丶
 * @Date: 2019-04-07 21:13
 * @Version: V1.0
 */
@Controller
@RequestMapping("/sign_in_rule")
public class SignInRuleController {

    @Autowired
    private SignInRuleService signInRuleService;

    @PostMapping("/add")
    @ResponseBody
    public ResultBody createSignInRule (@RequestBody SignRuleInRequestBody requestBody){
        String rule = signInRuleService.createRule(requestBody);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), rule);
    }

    @PostMapping("/query")
    @ResponseBody
    public ResultBody findAllRule (@RequestBody PageRequestBody requestBody){

        PageDTO<SignInRule> activityPageDTO = null;
        try {
            activityPageDTO = signInRuleService.allSignInRuleForManagement(requestBody);
        } catch (SignInRuleException e) {
            e.printStackTrace();
        }

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), activityPageDTO);
    }

    @PostMapping("/query_used")
    @ResponseBody
    public ResultBody findUsedRule (){
        SignInRule usedRule = signInRuleService.findUsedRule();
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), usedRule);
    }



    /**
     * 修改业态信息
     *
     * @param request
     * @return
     */
    @PostMapping("/update_sign_in_rule")
    @ResponseBody
    public ResultBody updateSignInRule(@RequestBody SignRuleInRequestBody request){
        if(StringUtils.isEmpty(request.getId())){
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明id");
        }
        boolean flag = signInRuleService.updateSignInRule(request);
        if (flag){
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "更新成功");
        }else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "更新失败");
        }
    }


    @PostMapping("/remove_sign_in_rule")
    @ResponseBody
    public ResultBody removeSignInRule ( @RequestBody SignRuleInRequestBody requestBody){
        if(StringUtils.isEmpty(requestBody.getId())){
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明id");
        }
        boolean flag = signInRuleService.removeSignInRule(requestBody);
        if (flag){
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "更新成功");
        }else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "更新失败");
        }
    }

    /**
     * 根据商户id查找签到积分规则
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_id")
    @ResponseBody
    public ResultBody queryById(@RequestBody SignRuleInRequestBody request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "输入的楼层id为空");
        }
        SignInRuleDTO signInRuleDTO = signInRuleService.query(request);
        if (signInRuleDTO == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无此楼层信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", signInRuleDTO);
    }

}
